<?php
/**
 * (jobs) 更新备货设置日期记录
 */
namespace App\Jobs\Basics;

use App\Models\SsBasic\AreaSku;
use App\Models\Stocking\StockingSetDate;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class TransportSeaDayRelate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $areaId;
    protected $seaDay;

    /**
     * TransportSeaDayRelate constructor.
     * @param $areaId
     * @param $seaDay
     */
    public function __construct($areaId,$seaDay)
    {
        $this->areaId = $areaId;
        $this->seaDay = $seaDay;
    }

    /**
     * Execute the job.
     * @return void
     */
    public function handle()
    {
        $this->exeFillData($this->areaId,$this->seaDay);
    }

    //更新备货设置日期记录
    private function exeFillData($areaId,$seaDay){
        StockingSetDate::query()->where('area_id',$areaId)->orderBy('id')
            ->chunk(50,function ($item) use ($seaDay){
            foreach ($item as $_item){
                $_item->sea_day = $seaDay;
                $_item->sea_available = AreaSku::getAvailable($_item->purchase_cycle,$_item->sea_day);
                $_item->status = StockingSetDate::setDateLe2Date($_item->set_date,$_item->sea_available);
                $_item->save();
            }
        });
    }
}
